<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
              @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan"
              collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.code" placeholder="标签分组代码" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.name" placeholder="标签分组名称" />
          </el-form-item>

          <el-form-item>
            <el-input v-model="query.params.storeId" placeholder="商铺编号" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.type" placeholder="类型">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button v-has="'GOODS_TAG_LIST'" type="primary" icon="el-icon-search" size="mini"
              @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button v-has="'GOODS_TAG_ADD'" type="success" icon="el-icon-plus" size="mini"
              @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>

    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="标签分组代码" width="110">
          <template slot-scope="scope">
            {{ scope.row.code }}
          </template>
        </el-table-column>
        <el-table-column label="标签分组名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="商铺编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.storeId }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="110">
          <template slot-scope="scope">
            {{ scope.row.status }}
          </template>
        </el-table-column>
        <el-table-column label="每行显示个数" width="110">
          <template slot-scope="scope">
            {{ scope.row.columns }}
          </template>
        </el-table-column>
        <el-table-column label="类型" width="110">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>
        <el-table-column label="所属组织" width="110">
          <template slot-scope="scope">
            {{ scope.row.organId }}
          </template>
        </el-table-column>
        <el-table-column label="所属租户" width="110">
          <template slot-scope="scope">
            {{ scope.row.tenantId }}
          </template>
        </el-table-column>
        <el-table-column label="排序" width="110">
          <template slot-scope="scope">
            {{ scope.row.sorted }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <el-link v-has="'GOODS_TAG_EDIT'" type="warning" icon="el-icon-edit" size="mini"
              @click="handleEdit(scope.$index, scope.row)">修改</el-link>
            <el-link v-has="'GOODS_TAG_REMOVE'" type="danger" icon="el-icon-plus" size="mini"
              @click="handleDelete(scope.$index, scope.row)">删除</el-link>

            <el-link v-has="'GOODS_TAG_GOODS_LIST'" type="primary" icon="el-icon-edit" size="mini"
              @click="handleShowGoodsTagGoodsList(scope.$index, scope.row)">查看商品</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>

    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="GoodsTagRules" ref="GoodsTagForm">
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps"
              :options="tenantOptions" @change="chooseTenantForm(form)" style="width:100%" clearable collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织" :props="props"
              :options="organFormOptions" @change="chooseOrganForm(form)" style="width:100%" collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="标签分组代码" required prop="code">
            <el-input v-model="form.code" placeholder="标签分组代码" auto-complete="off" />
          </el-form-item>
          <el-form-item label="标签分组名称" required prop="name">
            <el-input v-model="form.name" placeholder="标签分组名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="商铺编号" required prop="storeId">
            <el-input v-model="form.storeId" placeholder="商品编号" auto-complete="off" />
          </el-form-item>
          <el-form-item label="每行显示个数" required prop="columns">
            <el-input v-model="form.columns" placeholder="每行显示个数" auto-complete="off" />
          </el-form-item>
          <el-form-item label="排序" required prop="sorted">
            <el-input v-model="form.sorted" placeholder="排序" auto-complete="off" />
          </el-form-item>
          <el-form-item label="类型" required prop="type">
            <el-select v-model="form.type" placeholder="类型" style="width:100%">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>


    <div class="block">
      <!--弹框-->
      <el-dialog :title="'给标签绑定产品'" width="78%" :visible.sync="dialogGoodsTagRefVisible">
        <div class="block">
          <el-table ref="selectGoodsTable" v-loading="listLoading" :data="goodsList" element-loading-text="Loading"
            @selection-change="handleSelectionChange" border fit highlight-current-row>
            <el-table-column type="selection" width="45">
            </el-table-column>
            <el-table-column label="商品名称" width="110">
              <template slot-scope="scope">
                {{ scope.row.goodsName }}
              </template>
            </el-table-column>

            <el-table-column label="商品图片url" width="110">
              <template slot-scope="scope">
                <el-image style="width: 100px; height: 100px" :src="scope.row.goodsImg">
                </el-image>
              </template>
            </el-table-column>
            <el-table-column label="商品描述" width="110">
              <template slot-scope="scope">
                {{ scope.row.goodsDescribe }}
              </template>
            </el-table-column>
            <el-table-column label="供应商" width="110">
              <template slot-scope="scope">
                {{ scope.row.supplierId }}
              </template>
            </el-table-column>
            <el-table-column label="所属机构" width="110" prop="organId" :formatter="translateOrgan" />
            <el-table-column label="商品分类" width="110">
              <template slot-scope="scope">
                {{ scope.row.goodsTypeId }}
              </template>
            </el-table-column>
            <el-table-column label="商品规格单位" width="110">
              <template slot-scope="scope">
                {{ scope.row.unit }}
              </template>
            </el-table-column>
            <el-table-column label="商品规格值" width="110">
              <template slot-scope="scope">
                {{ scope.row.unitValue }}
              </template>
            </el-table-column>
            <el-table-column label="商品产地" width="110">
              <template slot-scope="scope">
                {{ scope.row.productionPlace }}
              </template>
            </el-table-column>
            <el-table-column label="备注" width="110">
              <template slot-scope="scope">
                {{ scope.row.remark }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination :current-page="query.goods.currentPage" :page-sizes="[10, 20, 30, 40]"
              :page-size="query.goods.pageSize" layout="total, sizes, prev, pager, next, jumper"
              :total="query.goods.total" @size-change="handleQueryGoodsSizeChange"
              @current-change="handleQueryGoodsCurrentChange" />
          </div>
        </div>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogGoodsTagRefVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleBindGoodsTagRef">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getList, queryForList, doDelete, doEdit } from '@/api/GoodsTag/'
import { batchBind as batchBindGoodsTagRef, queryForList as queryGoodsTagRefList } from '@/api/GoodsTagRef/'

import { getList as getOrganPager, querySubordinateOrgan, queryAssigneeOrgan } from '@/api/Organ'
import { getList as queryGoodsPricePage } from '@/api/GoodsPrice'
import { getList as queryGoodsPage } from '@/api/Goods'

import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode, getCurrentTimeYYYY_MM_dd_HH_mm_ss } from '@/api/utils'
import { getTenantList, getToken, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import { getGoodsTypeTree } from '@/api/GoodsType/'

import localStorageUtil from '@/utils/localStorageUtil'
import sessionStorageUtil from '@/utils/sessionStorageUtil'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      dialogGoodsTagRefVisible: false,
      goodsList: [],
      selectionsBindGoodsTag: [],
      tempGoodsTag: null,
      form: {
        code: '',
        createBy: '',
        createDate: '',
        features: '',
        id: '',
        isDeleted: '',
        columns: '',
        name: '',
        organId: '',
        organPath: '',
        parentId: '',
        remark: '',
        sorted: '',
        status: '',
        storeId: '',
        tenantId: '',
        type: '',
        updateBy: '',
        updateDate: '',
        'id': ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑'
      },
      GoodsTagRules: {
      },
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organTreeProps: {
        children: 'children',
        label: 'organName',
        url: 'url'
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organFormOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      tenantOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      supplierOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organQueryModel: 'selfAndSub',
      supplier: null, //供应商
      goodsType: null, //商品分类
      // // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      // 新增和编辑弹框标题
      dialogStatus: '',
      query: {
        params: {
          code: '', //标签分组代码,
          createBy: '', //创建者,
          createDate: '', //创建时间,
          features: '', //,
          id: '', //,
          isDeleted: '', //,
          name: '', //标签分组名称,
          organId: '', //所属组织,
          organPath: '', //组织路径,
          parentId: '', //,
          remark: '', //,
          sorted: '', //排序,
          status: '', //状态,
          storeId: '', //商品编号,
          tenantId: '', //所属租户,
          type: '', //类型,
          updateBy: '', //更新者,
          updateDate: '', //更新时间,

        },
        goods: {
          id: '',
          organId: '',
          organPath: '',
          organName: '',
          goodsTypeId: '',
          goodsTypePath: '',
          goodsName: '',
          supplierId: '',
          goodsImg: '',
          goodsDescribe: '',
          unit: '',
          unitValue: '',
          productionPlace: '',
          sys: '',
          isDeleted: '',
          quote: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: '',
          assignee: { organPath: '' },
          organIds: [],
          total: 5,
          currentPage: 1,
          pageSize: 10,
        },
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.initOrganFormTree()
    this.handleFiltTenant()
    this.fetchData()
  },
  methods: {
    translateOrgan(row) {
      return translateOrganId2OrganName(row);
    },
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        }
      }
      this.query.params.organId = this.query.params.organId || getOrganId()
      this.query.params.organPath = this.query.params.organPath || getOrganPath()
    },
    fetchData() {
      this.listLoading = true
      this.createOrganQuery()
      this.currentPage = this.currentPage || 1
      this.pageSize = this.pageSize || 10
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })
    },

    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.code = ''
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.features = '1'
      this.form.id = ''
      this.form.isDeleted = ''
      this.form.name = ''
      this.form.organId = getOrganId()
      this.form.organPath = getOrganPath()
      this.form.parentId = ''
      this.form.remark = ''
      this.form.sorted = ''
      this.form.status = '1'
      this.form.storeId = ''
      this.form.tenantId = getTenantId()
      this.form.type = '1'
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)
      this.form.code = row.code
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.features = row.features
      this.form.id = row.id
      this.form.isDeleted = row.isDeleted
      this.form.name = row.name
      this.form.organId = row.organId
      this.form.organPath = row.organPath
      this.form.parentId = row.parentId
      this.form.remark = row.remark
      this.form.sorted = row.sorted
      this.form.status = row.status
      this.form.storeId = row.storeId
      this.form.tenantId = row.tenantId
      this.form.type = row.type
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const data = response.data
          console.log(data)
          if (data.status == 1) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else if (data.status == 403) {
            this.$message({
              type: 'warning',
              message: data.description
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      doEdit(this.form).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function (item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      debugger
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function () {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(nodes) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      this.form.tenantId = null
      let that = this
      debugger
      if (nodesInfo.length) {
        this.form.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(nodes) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.form.organPath = nodesInfo[0].data.organPath
        this.form.organId = nodesInfo[0].data.id
        this.form.tenantId = nodesInfo[0].data.tenantId
      }
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function (item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    handleShowGoodsTagGoodsList(index, row) {
      this.dialogGoodsTagRefVisible = true
      this.tempGoodsTag = row
      let that = this
      this.query.goods.organId = row.organId || getOrganId()
      this.query.goods.organId = row.organId || getOrganId()
      this.handleQueryGoods()
    },
    handleQueryGoods() {
      let that = this
      this.query.goods.currentPage = this.query.goods.currentPage || 1
      this.query.goods.pageSize = this.query.goods.pageSize || 10
      this.listLoading = true
      queryGoodsPage(this.query.params, this.query.goods.currentPage, this.query.goods.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.query.goods.total = data.totalCount
        this.query.goods.pageSize = data.pageSize
        this.query.goods.currentPage = data.currentPage
        this.goodsList = data.entities

        if (this.goodsList && this.goodsList.length) {
          this.handleQueryHasRefGoods(this.goodsList, function () {

          })

        }
        that.listLoading = false
      })
    },
    handleQueryGoodsSizeChange(val) {
      this.query.goods.currentPage = 1
      this.query.goods.pageSize = val
      this.handleQueryGoods()
    },
    handleQueryGoodsCurrentChange(val) {
      this.query.goods.currentPage = val
      this.handleQueryGoods()

    },

    handleQueryHasRefGoods(goodsList, fn) {

      let that = this
      let goodsHasRefQuery = {
        goodsIds: [],
        goodsTagId: this.tempGoodsTag.id,
        tenantId: this.tempGoodsTag.tenantId || getTenantId(),
        organPath: this.tempGoodsTag.organPath || getOrganPath()
      }
      for (let index = 0; index < goodsList.length; index++) {
        const element = goodsList[index];
        goodsHasRefQuery.goodsIds.push(element.id);
      }
      //TODO 查询当前页的选中商品
      queryGoodsTagRefList(goodsHasRefQuery).then(response => {
        const gateway = response.data
        if (gateway.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          let datas = gateway.data

          let selectedOrigin = {}
          for (let index = 0; index < goodsList.length; index++) {
            const element = goodsList[index];
            selectedOrigin[element.id] = element
          }


          that.$nextTick(function () {
            datas.forEach(row => {
              debugger
              that.$refs.selectGoodsTable.toggleRowSelection(selectedOrigin[row.goodsId]);
            })
          })

        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }


      })
    },
    handleSelectionChange(selections) {
      this.selectionsBindGoodsTag = selections
    },
    handleBindGoodsTagRef() {
      if (!this.selectionsBindGoodsTag.length) {
        this.$message({
          type: 'info',
          message: "请选择需要绑定的商品产品！"
        })
        return
      }
      let batchDto = {
        tenantId: this.tempGoodsTag.tenantId || getTenantId(),
        id: this.tempGoodsTag.id, //GoodsTag.id
        entities: []
      }

      for (let index = 0; index < this.selectionsBindGoodsTag.length; index++) {
        const goods = this.selectionsBindGoodsTag[index];
        let goodsTagRef = {
          createBy: getCurrentLoginUserId(),
          createDate: getCurrentTimeYYYY_MM_dd_HH_mm_ss(),
          goodsId: goods.id,
          organId: getOrganId(),
          organPath: getOrganPath(),
          createBy: getCurrentLoginUserId(),
          tenantId: getTenantId(),
          sorted: '',
          goodsTagId: this.tempGoodsTag.id,
          storeId: this.tempGoodsTag.storeId,
          status: '1',
          updateBy: getCurrentLoginUserId(),
          updateDate: getCurrentTimeYYYY_MM_dd_HH_mm_ss(),
        }
        batchDto.entities.push(goodsTagRef)
      }

      batchBindGoodsTagRef(batchDto).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
      })
    }

  }
}
</script>
